import React, { Component } from "react";

class Form extends Component {
  state = {
    textValue: "",
  };

  handleChange = (e) => {
    this.setState({
      textValue: e.target.value,
    });
  };

  handleClick = () => {
    // console.log(this.state.textValue);
    // 添加，是添加到父组件
    // 去父组件的事件
    if (this.state.textValue) {
      this.props.onAdd(this.state.textValue);
      this.setState({
        textValue: "",
      });
    }
  };

  render() {
    const { textValue } = this.state;
    return (
      <>
        <input type="text" value={textValue} onChange={this.handleChange} />
        <button onClick={this.handleClick}>添加</button>
      </>
    );
  }
}

export default Form;
